package com.huilian.iotbox.admin.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.activity.dao.ActivityDao;
import com.huilian.iotbox.admin.activity.service.ActivityService;
import com.huilian.iotbox.admin.producer.RabbitProducer;
import com.huilian.iotbox.data.dto.ActivityDto;
import com.huilian.iotbox.data.dto.ActivityOrderDto;
import com.huilian.iotbox.data.enums.ActivityStateEnum;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.vo.ActivityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhiquan
 * @CreateDate: 2022-06-06 17:18
 * @Description:
 */
@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private RabbitProducer rabbitProducer;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;

    /**
     *
     * @param stateArray
     */
    @Override
    public void findAllActivityByBeginAndEnd(int[] stateArray) {
        List<ActivityVo> activityVoList = activityDao.findAllActivityByBeginAndEnd(stateArray);

        for (ActivityVo activityVo : activityVoList) {
            try {
                Map<String, String> delayMap = new HashMap<>();
                delayMap.put("activityId", activityVo.getId().toString());
                if (ActivityStateEnum.NOT_STARTED.getCode().compareTo(activityVo.getStatus()) == 0) {
                    Long activityStartTime = activityVo.getStartTime().getTime() - System.currentTimeMillis();
                    int intValue = activityStartTime.intValue();
                    log.info("每天定时任务发送检测-开始活动队列（延迟），activityId={}，延迟{}ms", activityVo.getId(), intValue);
                    //没开始的活动，加入延时队列处理活动状态刷新
                    rabbitProducer.sendReplayActivityStartStateLazyQueue(delayMap, intValue);
                } else if (ActivityStateEnum.UNDERWAY.getCode().compareTo(activityVo.getStatus()) == 0) {

                    Long activityEndTime = activityVo.getEndTime().getTime() - System.currentTimeMillis();
                    int intValue = activityEndTime.intValue();
                    log.info("每天定时任务发送检测-结束活动队列（延迟），activityId={}，延迟{}ms", activityVo.getId(), intValue);
                    rabbitProducer.sendReplayActivityEndStateLazyQueue(delayMap, intValue);
                } else {
                    errorLogCommonService.writerInfoErrorLog("活动开始、结束状态任务。活动状态异常", activityVo);
                }
            } catch (Exception e) {
                e.printStackTrace();
                errorLogCommonService.writerInfoException("活动开始、结束状态任务。处理活动状态异常", activityVo, e);
                continue;
            }
        }
    }


    @Override
    public List<ActivityVo> findSuccessTotalAmountByDeviceList(ActivityOrderDto activityOrderDto) {
        return activityDao.findSuccessTotalAmountByDeviceList(activityOrderDto);
    }
}
